package com.lanou.tipsy.fragment.main;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.lanou.tipsy.R;
import com.lanou.tipsy.adapter.main.MainViewPagerFragmentAdapter;
import com.lanou.tipsy.base.BaseFragment;
import com.lanou.tipsy.fragment.search.SearchFragment;

/**
 * 主页  乐库标签下 第二层 RadioGroup
 * Create by FengJun
 * EE
 */
public class MainFragment extends BaseFragment implements
        ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener, View.OnClickListener {
    //定义viewPager对象
    private ViewPager vp_content;
    private RadioGroup rg_main_tab;
    private RadioButton mineRb, recommendRb, singerRb,
            rankRb, songsListRb, mvRb;
    private ImageView searchRb;
    private MainViewPagerFragmentAdapter viewPagerFragmentAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fr_main, null);
        initView(view);
        return view;
    }

    /**
     * 绑定布局   初始化等
     *
     * @param view
     */
    private void initView(View view) {
        //初始化viewPager
        vp_content = (ViewPager) view.findViewById(R.id.vp_content);
        //初始化RadiosGroup
        rg_main_tab = (RadioGroup) view.findViewById(R.id.rg_main_tab);
        mineRb = (RadioButton) view.findViewById(R.id.rb_mine);
        recommendRb = (RadioButton) view.findViewById(R.id.rb_recommend);
        singerRb = (RadioButton) view.findViewById(R.id.rb_singers);
        rankRb = (RadioButton) view.findViewById(R.id.rb_rank);
        songsListRb = (RadioButton) view.findViewById(R.id.rb_songs_list);
        mvRb = (RadioButton) view.findViewById(R.id.rb_mv);
        searchRb = (ImageView) view.findViewById(R.id.rb_search);
        searchRb.setOnClickListener(this);

        viewPagerFragmentAdapter = new MainViewPagerFragmentAdapter(getChildFragmentManager());
        vp_content.setAdapter(viewPagerFragmentAdapter);
        vp_content.setCurrentItem(1);    //启动默认显示推荐

        //设置监听
        rg_main_tab.setOnCheckedChangeListener(this);
        vp_content.addOnPageChangeListener(this);
    }

    /**
     * RadioButton  点击监听
     *
     * @param group
     * @param checkedId
     */
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_mine:
                vp_content.setCurrentItem(0);
                break;
            case R.id.rb_recommend:
                vp_content.setCurrentItem(1);
                break;
            case R.id.rb_singers:
                vp_content.setCurrentItem(2);
                break;
            case R.id.rb_rank:
                vp_content.setCurrentItem(3);
                break;
            case R.id.rb_songs_list:
                vp_content.setCurrentItem(4);
                break;
            case R.id.rb_mv:
                vp_content.setCurrentItem(5);
                break;
        }
    }


    /**
     * RadioGroup    ViewPager  滑动监听
     *
     * @param position
     * @param positionOffset
     * @param positionOffsetPixels
     */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                mineRb.setChecked(true);
                break;
            case 1:
                recommendRb.setChecked(true);
                break;
            case 2:
                singerRb.setChecked(true);
                break;
            case 3:
                rankRb.setChecked(true);
                break;
            case 4:
                songsListRb.setChecked(true);
                break;
            case 5:
                mvRb.setChecked(true);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        // state的状态有三个，0表示什么都没做，1正在滑动，2滑动完毕
        /*if (state == 2) {
            switch (vp_content.getCurrentItem()) {
                default:
                    break;
            }
        }*/
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rb_search:
                vp_content.setCurrentItem(6);
                //初始化
                FragmentManager manager = getActivity().getSupportFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.replace(R.id.fl_main, new SearchFragment());
                //transaction.addToBackStack(null);
                transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                transaction.commit();
                break;
        }
    }
}

